﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace NASA.BeAMartian.Utils
{
    public static class VisualTreeUtils
    {
        public static T FindChildByType<T>(DependencyObject parent) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    return (child as T);
                }
                DependencyObject found = FindChildByType<T>(child);
                if (found != null)
                {
                    return (found as T);
                }
            }
            throw new ArgumentException("Cannot find child of specified type in parent.");
        }

        public static T FindParentByType<T>(FrameworkElement descendent) where T : class
        {
            DependencyObject obj = descendent;
            while (!(obj is T) && obj != null)
            {
                obj = VisualTreeHelper.GetParent(descendent);
                descendent = obj as FrameworkElement;
            }

            return (obj as T);
        }

        /// <summary>
        /// There is a bug in Silverlight in which an ItemsControl does not allow the RadioButton Group 
        /// Name to function properly. This works around taht issues.
        /// </summary>
        /// <param name="container">The container (ItemsControl)</param>
        /// <param name="groupName">The GroupName we are considering</param>
        /// <param name="ignoreTag">The id of our button so we do not uncheck it.</param>
        public static void UncheckGroupRadioButtons(FrameworkElement container, string groupName, object ignoreTag)
        {

            int childCount = VisualTreeHelper.GetChildrenCount(container);
            for (int i = 0; i < childCount; i++)
            {
                DependencyObject obj = VisualTreeHelper.GetChild(container, i);
                if (obj is RadioButton)
                {
                    RadioButton curRadio = (RadioButton)obj;
                    if (curRadio.GroupName == groupName && curRadio.Tag != ignoreTag)
                        curRadio.IsChecked = false;
                }
                else
                {
                    UncheckGroupRadioButtons((FrameworkElement)obj, groupName, ignoreTag);
                }
            }
        }

        /// <summary>
        /// Check the RadoButton with the given tag.
        /// </summary>
        /// <param name="container">The container (ItemsControl)</param>
        /// <param name="groupName">The GroupName we are considering</param>
        /// <param name="tag">The id of our button that we want to check.</param>
        public static void CheckGroupRadioButton(FrameworkElement container, string groupName, object tag)
        {

            int childCount = VisualTreeHelper.GetChildrenCount(container);
            for (int i = 0; i < childCount; i++)
            {
                DependencyObject obj = VisualTreeHelper.GetChild(container, i);
                if (obj is RadioButton)
                {
                    RadioButton curRadio = (RadioButton)obj;
                    if (curRadio.GroupName == groupName && curRadio.Tag == tag)
                    {
                        curRadio.IsChecked = true;
                        break;
                    }
                }
            }
        }
    }
}
